package cn.edu.nju.contact.contacts;

import java.util.List;

import cn.edu.nju.contact.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

public class ContactListAdapter extends ArrayAdapter<ContactItem> {
	public ContactListAdapter(Activity activity, List<ContactItem> listItems) {
		super(activity, 0, listItems);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) getContext();

		View rowView = convertView;
		ContactItemCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.contact_list_item, null);
			viewCache = new ContactItemCache(rowView, activity);
			rowView.setTag(viewCache);
		} else {
			viewCache = (ContactItemCache) rowView.getTag();
		}

		ContactItem contactItem = getItem(position);

		
		
		
		viewCache.getBodyView(contactItem.getName(), contactItem.getPhoneNum());

		viewCache.getBtnView();

		ImageView imageView = viewCache.getImageView();
		Uri imageUri = contactItem.getImaUri();
		if (imageUri == null) {
			imageView.setBackgroundResource(R.drawable.dark_dragdefault);
		} else {
			Bitmap bitmap = BitmapFactory.decodeFile(imageUri.getPath());
			imageView.setBackgroundDrawable(new BitmapDrawable(bitmap));
		}

		return rowView;
	}
}
